package com.healthy.service;

import com.healthy.entity.Customerprofile;
import com.healthy.entity.User;
import com.healthy.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserService {

//    @Autowired
//    UserDao userDao;

    @Autowired
    UserMapper userMapper;

    //    查询全部
    public List<User> cuUser() {
        return userMapper.cuUser();
    }

    public User register(User user) {
        User oldUser = userMapper.getUserByAccount(user.getAccount());
        if (oldUser == null) {
            userMapper.saveUser(user);
            System.out.println("用户ID是" + user.getId());
            return user;
        } else {
            return null;
        }
    }

    public User login(User user) {
        User loginUser = userMapper.getUserByAccount(user.getAccount());
        if (loginUser == null) {
            return null;
        } else {
            if (user.getPassword() != null && user.getPassword().equals(loginUser.getPassword())) {
                String token = UUID.randomUUID().toString();
                loginUser.setToken(token);
                userMapper.setTokenToUser(token, loginUser.getId());
                return loginUser;
            }
        }
        return null;
    }

    public User getUserByToken(String token) {
        return userMapper.getUserByToken(token);
    }

    //    删除
    public int cuDeleteByid(int id) throws Exception {
        int res = userMapper.cuDeleteByid(id);
        if (res > 0) {
            return 1;
        } else {
            return 0;
        }
    }
    //    修改
    public int cuUpdateByid(int id,String account,String name) throws Exception {
        int res = userMapper.cuUpdateByid(id,account,name);
        if (res > 0) {
            return 1;
        } else {
            return 0;
        }
    }
}
